﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Data;
using App.Core.Extensions;
using System.IO;
using System.Data;
using App.Core.DB;
using System.Text;

public partial class Pages_Private_MemberShipDetails : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            BindMembershipInfo();
        }
    }

    protected void BindPageInfo()
    {
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }

    protected void BindMembershipInfo() {

        String table = @"<table cellpadding='3' cellspacing='0' width='100%' class='GridView'>
                                <colgroup>
                                    <col style='width:20%;' />
                                    <col />
                                </colgroup>
                                {0}
                            </table>";
        StringBuilder sb = new StringBuilder();

        UtilityDAO dao = new UtilityDAO();
        App.Data.DbParameter[] parameters = new[] {
            new App.Data.DbParameter("@CustomerID", SessionCache.CurrentUser.CustomerID)           
            };

        DataSet ds = dao.GetDataSet(AppSQL.GET_PACKAGE_DETAILS_BY_CUSTOMER, parameters, false);
        decimal pkPrice = 0;
        if (!ds.IsEmpty())
        {
            DataTable dt = ds.Tables[0];
            
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                int PL = NullHandler.GetInt32(row["ProductCount"]);
                int PS = NullHandler.GetInt32(row["ProjectSubmitted"]);
                pkPrice = NullHandler.GetDecimal(row["Price"]);

                sb.AppendFormat("<tr><td class='bold'>Package Name:</td> <td>{0}</td></tr>", NullHandler.GetString(row["Name"]));
                sb.AppendFormat("<tr><td class='bold'>Duration:</td> <td>{0}</td></tr>", NullHandler.GetInt32(row["Duration"]));
                sb.AppendFormat("<tr><td class='bold'>Start Date:</td> <td>{0}</td></tr>", NullHandler.GetDateTime(row["StartDate"]).ToString(AppConstants.ValueOf.DATE_FROMAT_DISPLAY));
                sb.AppendFormat("<tr><td class='bold'>Expire Date:</td> <td>{0}</td></tr>", NullHandler.GetDateTime(row["ExpireDate"]).ToString(AppConstants.ValueOf.DATE_FROMAT_DISPLAY));
                sb.AppendFormat("<tr><td class='bold'>Project Limit:</td> <td>{0}</td></tr>", NullHandler.GetInt32(row["ProductCount"]));
                sb.AppendFormat("<tr><td class='bold'>Project Submitted:</td> <td><a href='/Pages/Private/ProjectList.aspx'>{0}</a></td></tr>", NullHandler.GetInt32(row["ProjectSubmitted"]));
                sb.AppendFormat("<tr><td class='bold'>Project Remaining:</td> <td>{0}</td></tr>", PL-PS);
            }

            ltrMembershipDetails.Text = String.Format(table, sb.ToString());            

        }

        BtcHousingEntities context = new BtcHousingEntities();
        var subscription = context.PackageSubscriptions.FirstOrDefault(P => P.CustomerID == SessionCache.CurrentUser.CustomerID && P.ExpireDate >= DateTime.Now && P.IsActiveSubscription); // Active Subscriber with a package, Date not Expired

        if (subscription == null)
        {
            divMembershipExpired.Visible = true;
            divFreeMembership.Visible = false;
        }        
        else if ( pkPrice < 1 ) {
            divMembershipExpired.Visible = false;
            divFreeMembership.Visible = true;
        }
        
    }

}